/**
 * 将 url 的页面参数转换为对象
 * 
 * @param {string} url 
 */
export function getRequestParams(url) {
    const search = getSearchFromUrl(url);
    if (!search) return {};

    const tokens = search.split('&');
    const result = {};
    for (let i = 0; i < tokens.length; i++) {
        const [key, value] = tokens[i].split('=');
        result[key] = value;
    }

    return result;
}

/**
 * 从 url 中截取页面参数字符串，字符串不包含问号
 * 
 * @param {string} url 
 */
export function getSearchFromUrl(url) {
    if (!url) return '';
    
    const questionIndex = url.indexOf('?');
    if (questionIndex === -1) return '';

    const hashIndex = url.indexOf('#');
    if (hashIndex === -1) return url.slice(questionIndex+1);

    if (hashIndex < questionIndex) return '';

    return url.slice(questionIndex+1, hashIndex);
}